<?php
class user_model extends CI_Model{
	
	function register_user($username, $password, $fname, $lname, $email)
	{
		//input user into user
		
		//encrypted password
		$sha1_password = sha1($password);
		
		$query_str = "INSERT INTO user (user_username, user_password, user_fname, user_lname, user_email) VALUES (?, ?, ?, ?, ?)";
		
		$this->db->query($query_str, array($username, $sha1_password, $fname, $lname, $email));
		
	}
	
	function check_username($username)
	{
		//username checking
		$query_str = 'SELECT user_username from user where user_username = ?';
		
		$result = $this->db->query($query_str, $username);
		
		if ($result->num_rows() > 0)
		{
			//exist
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	function check_email($email)
	{
		//email checking 
		$query_str = 'SELECT user_email from user where user_email = ?';
		
		$result = $this->db->query($query_str, $email);
		
		if ($result->num_rows() > 0)
		{
			//exist
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	
	
	
	
	function check_login($username, $password)
	{
		//login check to database 
		
		$sha1_password = sha1($password);
		
		$query_str = "SELECT * FROM user WHERE user_username = ? and user_password = ?";
		
		$result = $this->db->query($query_str, array($username, $sha1_password))->result();
		
		if (count($result) == 1)
		{
			return 0;
		}
		else 
		{
			//wrong password 
			return 1;
		}
	
	}
	
	function getUserByUsername($username){
		//input all user data from database
		$users = $this->db->get_where('user', array('user_username' => $username))->result();
		return $users[0];
	}
	
	
	
	
}

?>